define(['app'], function (app) {
    app.controller('BookNoteModelCtrl', ['$scope', '$state', '$stateParams', '$http', '$q', 'nodeService','$filter',
        function ($scope, $state, $stateParams, $http, $q, nodeService,$filter) {

            $scope.entity = {};
            var id = $stateParams.id;
            var type = $stateParams.type;

            $scope.title = '添加';
            $scope.viewFlag = false;

            //获取单个阅读笔记
            $scope.getNodeDetail = function (id) {
                nodeService.getNodeDetail(id).then(
                    function (response) {
                        if (response.status == 200) {
                            if (response.data != null) {
                                if (response.data.beginTime != null)
                                    response.data.beginTime = getLocalTime(response.data.beginTime);
                                if (response.data.endTime != null)
                                    response.data.endTime = getLocalTime(response.data.endTime);
                            }
                            $scope.entity = response.data;
                        }

                        else {
                            alert("查询数据失败!");
                        }
                    },
                    function (error) {
                        alert("查询数据失败！");
                    }
                );
            }
            //首次加载，获取所有图书信息
            $scope.getbookAll = function () {
                nodeService.getbookAll().then(
                    function (response) {
                        if (response.status == 1) {
                            $scope.bookList = response.data;
                        } else {
                        }
                    },
                    function (error) {
                    }
                );
            };
            //调用获取图书方法
            $scope.getbookAll();
            if (id != null && id != '') {
                if (type == 3) {
                    $scope.title = '查看';
                    $scope.viewFlag = true;
                }
                else {
                    $scope.title = '编辑';
                    $scope.viewFlag = false;
                }
                $scope.getNodeDetail(id);
            }
            $scope.save = function () {
                if ($scope.entity.bookId == '' || $scope.entity.bookId == null) {
                    alert("请选择图书！");
                    return;
                }
                if ($scope.entity.title == '' || $scope.entity.title == null) {
                    alert("请输入笔记标题！");
                    return;
                }
                if ($scope.entity.content == '' || $scope.entity.content == null) {
                    alert("请输入笔记内容！");
                    return;
                }
                if ($scope.entity.beginTime != null && $scope.entity.beginTime != '') {
                    if ($scope.entity.endTime != null && $scope.entity.endTime != '') {
                        var begin = new Date($scope.entity.beginTime.toString());
                        var end = new Date($scope.entity.endTime.toString());
                        if (begin >= end) {
                            alert("阅读开始时间必须小于结束时间！");
                            return;
                        }
                    }
                }
                $scope.entity.beginTime =$filter('date')($scope.entity.beginTime,"yyyy-MM-dd hh:mm:ss");
                $scope.entity.endTime = $filter('date')($scope.entity.endTime, "yyyy-MM-dd hh:mm:ss");
                if (id == null || id == '') {
                    nodeService.createNode($scope.entity).then(
                        function (response) {
                            if (response.status == 200) {
                                alert("保存成功！");
                                $state.go("book.note.list");
                            }
                            else
                                alert("保存失败！");
                        },
                        function (error) {
                            alert("保存失败！");
                            $state.go("book.note.list");
                        }
                    );
                }
                else {
                    nodeService.updateNode($scope.entity).then(
                       function (response) {
                           if (response.status == 200) {
                               alert("编辑成功！");
                               $state.go("book.note.list");
                           }
                           else
                               alert("编辑失败！");
                       },
                       function (error) {
                           alert("编辑失败！");
                           $state.go("book.note.list");
                       }
                   );
                }
            }
            $scope.cancel = function () {
                $state.go("book.note.list");
            }


        }]);
});